package com.atguigu.filter;


import com.atguigu.utils.JDBCTools;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebFilter({"*.jsp", "/delivery/*", "/user/password", "/user/logout"})
public class JDBCFilter extends HttpFilter {
    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = request.getSession();
        Object user = session.getAttribute("Session_User");
        //判断用户是否为登录状态
        if (user == null)
            response.sendRedirect("/login.html");
        else {
            try {
                chain.doFilter(request, response);
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                JDBCTools.releaseConnection();
            }
        }
    }
}
